﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.Managers.CApExportToPlmxFileManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.IO;
using Tecnomatix.Engineering.Implementation;


namespace DnProcessSimulateCommands.ExportToPLMX.Managers;

public class CApExportToPlmxFileManager
{
  private const string m_tempFolderName = "PLMX";
  internal const string m_sceneDataFolderName = "scene_data";
  internal const string m_sceneFileName = "scene_data";
  internal const string m_sceneFileExtension = ".js";
  internal const string m_simulationFileName = "simulation";
  internal const string m_simulationFileExtension = ".json";
  private string m_tempExportFolderPath = string.Empty;
  private string m_sceneFolderPath = string.Empty;
  private string m_sceneFileFullPath = string.Empty;
  private string m_simulationFileFullPath = string.Empty;
  private string m_destinationFilePath = string.Empty;

  public CApExportToPlmxFileManager(string plmxLocationFullPath)
  {
    this.m_tempExportFolderPath = Path.GetTempPath() + "PLMX";
    this.m_sceneFolderPath = this.m_tempExportFolderPath + "\\scene_data";
    this.m_sceneFileFullPath = this.m_sceneFolderPath + "\\scene_data.js";
    this.m_simulationFileFullPath = this.m_sceneFolderPath + "\\simulation.json";
    this.m_destinationFilePath = plmxLocationFullPath;
  }

  public bool CreateTempFolder()
  {
    try
    {
      this.RemoveTempFolder();
      Directory.CreateDirectory(this.m_tempExportFolderPath);
      Directory.CreateDirectory(this.m_sceneFolderPath);
    }
    catch (Exception ex)
    {
      CCoExportToPLMXUtils.ShowErrorMessage(ex.Message);
      return false;
    }
    return true;
  }

  public void RemoveTempFolder() => this.RemoveFolder(this.m_tempExportFolderPath);

  public void RemoveFolder(string folderPathToRemove)
  {
    if (!Directory.Exists(folderPathToRemove))
      return;
    Directory.Delete(folderPathToRemove, true);
  }

  public void CreateFile(string fileFullPath)
  {
    if (File.Exists(fileFullPath))
      File.Delete(fileFullPath);
    File.Create(fileFullPath);
  }

  public void CreatePlmx()
  {
    if (!Directory.Exists(this.m_tempExportFolderPath))
      throw new Exception("Export folder doesn't exists");
    if (TxZipUtilitiesImpl.ZipDirectoryContentToPath("scene_data", "scene_data.zip"))
      this.RemoveFolder("scene_data");
    if (!TxZipUtilitiesImpl.ZipDirectoryContentToPath(this.m_tempExportFolderPath, this.m_destinationFilePath))
      return;
    this.RemoveTempFolder();
  }

  public string TempRootExportFolder => this.m_tempExportFolderPath;

  public string SceneExportPath => this.m_sceneFileFullPath;

  public string SimulationExportPath => this.m_simulationFileFullPath;
}
